const app = getApp();
const companyName = app.globalData.companyName;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    clubPictures: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    const clubPicturesJson = wx.getStorageSync('clubPictures');
    const clubPictures = JSON.parse(clubPicturesJson);
    this.setData({
      clubPictures
    });
  },
  deleteById: function(e) {
    const id = e.currentTarget.dataset.id;
    const that = this;
    wx.showModal({
      title: '提示',
      content: '确定删除此照片？',
      success(res) {
        if (res.confirm) {
          that.confirmDelete(id);
        }
      }
    });
  },
  confirmDelete: function(id) {
    wx.showNavigationBarLoading();
    const url = app.globalData.url;
    const that = this;
    wx.request({
      url: url + '/management/deletePicture/' + id,
      success: function(response) {
        wx.stopPullDownRefresh();
        wx.hideNavigationBarLoading();
        if (response.data.success) {
          that.reflesh();
        } else {
          wx.showToast({
            title: '删除失败',
            icon: 'none'
          });
        }
      }
    })
  },
  reflesh: function() {
    wx.showNavigationBarLoading();
    const url = app.globalData.url;
    const that = this
    wx.request({
      url: url + '/management/getClub',
      data: {
        companyName
      },
      method: 'POST',
      success: function(response) {
        wx.stopPullDownRefresh();
        wx.hideNavigationBarLoading();
        if (response.data.success) {
          that.setData({
            clubPictures: response.data.content.clubPictures || []
          });
        } else {
          wx.showToast({
            title: response.data.message,
            icon: 'none'
          })
        }
      }
    })
  },
  uploadPicture: function() {
    if(this.data.clubPictures.length>=6){
      wx.showToast({
        title: '最多只能上传6张',
        icon: 'none'
      });
      return;
    }
    const that = this;
    const openId = app.globalData.openId;
    const url = app.globalData.url;
    const baseUrl = app.globalData.baseUrl;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        const tempFilePaths = res.tempFilePaths;
        const tempFile = res.tempFiles[0];
        const size = tempFile.size;
        if (size > 1600 * 1000) {
          wx.showToast({
            title: '图片不要大于1.5M',
            icon: 'none'
          });
          return;
        }
        wx.showNavigationBarLoading();
        wx.uploadFile({
          url: url + '/management/uploadPicture',
          filePath: tempFilePaths[0],
          name: 'file',
          formData: {
            companyName,
            picType: 'COMPANY'
          },
          method: 'POST',
          success: function(response) {
            wx.hideNavigationBarLoading();
            const returnObj = JSON.parse(response.data);
            if (returnObj.success) {
              that.reflesh();
            } else {
              wx.showToast({
                title: returnObj.message,
                icon: 'none'
              })
            }
          }
        })
      }
    })
  }
})